package com.bluesky.plum.uimodels.render.swing.components;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.uimodels.standard.components.EventListener;

/**
 * the core domain model is POJO. the rich domain object can be converted to and
 * from the POJO model. So, we want the UI change will automatically update the
 * POJO model through the rich domain model.
 * 
 * @author HZ00260
 * 
 */
public class SSpinBox extends
		com.bluesky.plum.uimodels.standard.components.SpinBox {

	private JTextField textField;

	public SSpinBox(RichDomainField field) {
		super(field);

		textField = new JTextField(String.valueOf(getField().value));

		textField.setToolTipText(field.description);
	}

	@Override
	public Object getNativeComponent() {

		return textField;
	}

	@Override
	public Integer getValue() {
		return Integer.valueOf(textField.getText());

	}

	@Override
	public void setValue(Integer value) {

		textField.setText(value.toString());
	}

	@Override
	public void setValueChangeEventListener(
			EventListener valueChangeEventListener) {

		super.setValueChangeEventListener(valueChangeEventListener);

		textField.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void changedUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();

			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();

			}
		});
	}
}
